package com.yilin.platform.base_core.commons.utils;

import org.springframework.util.StreamUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class StreamUtil {

    /**
     * 读取流数据
     * */
    public static String getBodyString(BufferedReader body){
        String str;
        StringBuilder wholeStr = new StringBuilder();
        try {
            while ((str = body.readLine()) != null) wholeStr.append(str);
            body.close();
            return wholeStr.toString();
        }catch (IOException e) {
            return null;
        }
    }

    public static String getBodyString(InputStream body) {
        return getBodyString( body, StandardCharsets.UTF_8);
    }

    public static String getBodyString(InputStream body, Charset charset){
        BufferedReader wholeStr = new BufferedReader(new InputStreamReader(body,charset));
        return getBodyString(wholeStr);
    }




}
